package me.bzcoder.easywebview.rich

import android.content.Context
import android.util.Log
import android.webkit.JavascriptInterface
import android.widget.Toast

/**
 * 用于JavaScript交互
 * @author : BaoZhou
 * @date : 2019/3/29 17:39
 */
class RichJavascriptInterface {
    private val TAG = "JavaScriptLog"
    private var context: Context? = null
    private var callBack: JavaScriptCallBack? = null
    private var clickImageCallBack: ClickImageCallBack? = null
    private var checkStyleCallBack: CheckStyleCallBack? = null
    private var clickEditLinkCallBack: ClickEditLinkCallBack? = null
    private var ImageListCallBack: GetImageListCallBack? = null

    constructor(context: Context?, callBack: JavaScriptCallBack?) {
        this.callBack = callBack
        this.context = context
    }

    constructor(context: Context?, callBack: ClickImageCallBack?) {
        clickImageCallBack = callBack
        this.context = context
    }

    /**
     * 样式识别回调
     * @param callBack
     */
    fun setCheckStyleCallBack(callBack: CheckStyleCallBack?) {
        checkStyleCallBack = callBack
    }

    /**
     * 点击图片回调
     * @param callBack
     */
    fun setClickImageCallBack(callBack: ClickImageCallBack?) {
        clickImageCallBack = callBack
    }

    /**
     * 编辑页面点击超链接回调
     * @param callBack
     */
    fun setClickEditLinkCallBack(callBack: ClickEditLinkCallBack?) {
        clickEditLinkCallBack = callBack
    }

    /**
     * 获取图片数组回调
     * @param imageListCallBack
     */
    fun setImageListCallBack(imageListCallBack: GetImageListCallBack?) {
        ImageListCallBack = imageListCallBack
    }

    fun setCallBack(callBack: JavaScriptCallBack?) {
        this.callBack = callBack
    }

    constructor() {}

    /**
     * 点击图片
     */
    interface JavaScriptCallBack {
        fun clickWebView()
    }

    /**
     * 样式识别回调
     */
    interface CheckStyleCallBack {
        fun checkStyle(json: String?)
    }

    /**
     * 点击图片
     */
    interface ClickImageCallBack {
        fun clickImage(src: String?, position: Int)
    }

    interface ClickEditLinkCallBack {
        fun clickEditLink(url: String?, title: String?)
    }

    /**
     * 获取图片数组
     */
    interface GetImageListCallBack {
        fun getImageList(imagesUrls: Array<String?>?)
    }

    constructor(context: Context?) {
        this.context = context
    }

    @JavascriptInterface
    fun toast(message: String) {
        Toast.makeText(context, "js:$message", Toast.LENGTH_SHORT).show()
    }

    @JavascriptInterface
    fun log(result: String) {
        Log.v(TAG, "js:$result")
    }

    /**
     * 点击富文本
     */
    @JavascriptInterface
    fun clickWebView() {
        if (callBack != null) {
            callBack!!.clickWebView()
        }
    }

    /**
     * 点击图片
     */
    @JavascriptInterface
    fun clickImage(src: String, position: String) {
        Log.v(TAG, "webview点击图片:" + src + "position:" + position)
        if (clickImageCallBack != null) {
            clickImageCallBack!!.clickImage(src, position.toInt())
        }
    }

    /**
     * 传递样式识别数据回来
     * @param json
     */
    @JavascriptInterface
    fun checkStyle(json: String) {
        Log.v(TAG, "checkStyle:$json")
        if (checkStyleCallBack != null) {
            checkStyleCallBack!!.checkStyle(json)
        }
    }

    /**
     * 编辑时候点击超链接
     * @param link
     * @param title
     */
    @JavascriptInterface
    fun clickEditLink(link: String, title: String) {
        Log.v(TAG, "clickEditLink:$link  $title")
        if (clickEditLinkCallBack != null) {
            clickEditLinkCallBack!!.clickEditLink(link, title)
        }
    }

    @JavascriptInterface
    fun getImageList(urls: Array<String?>) {
        Log.v(TAG, "getImageList:" + urls.size)
        if (ImageListCallBack != null) {
            ImageListCallBack!!.getImageList(urls)
        }
    }
}